﻿Imports System.Text

Namespace Items

    ''' <summary>
    ''' Represents an item.
    ''' </summary>
    Public MustInherit Class BaseItem

        ''' <summary>
        ''' Gets or sets the title of the item.
        ''' </summary>
        ''' <value>The title of the item.</value>
        Public Property Title() As String = String.Empty

        ''' <summary>
        ''' Gets or sets the parts that make up the item.
        ''' </summary>
        ''' <value>The parts (or "verses") that make up the item.</value>
        Public Property Parts() As List(Of String)=New List(Of String)

        ''' <summary>
        ''' Gets or sets the copyright line.
        ''' </summary>
        ''' <value>The copyright line. This can be more than one line.</value>
        Public Property CopyrightLine() As String = String.Empty

        ''' <summary>
        ''' Splits the item into verses, assuming each verse has been seperated with a blank line.
        ''' </summary>
        ''' <param name="lyrics">A list of lines that make up an item, seperated by blank lines.</param>
        ''' <returns>A list of verses that make up the item.</returns>
        Public Shared Function SplitIntoParts(ByVal lyrics As String()) As List(Of String)

            Dim Result As New List(Of String)
            Dim ActivePart As String = ""
            For I As Integer = 0 To lyrics.Length - 1
                Dim Line As String = lyrics(I)
                If Line.Trim <> "" Then
                    ActivePart += Line.Trim
                    If I < lyrics.Length - 1 AndAlso lyrics(I + 1).Trim <> "" Then ActivePart += Environment.NewLine
                ElseIf ActivePart <> "" Then
                    Result.Add(ActivePart)
                    ActivePart = ""
                End If
            Next
            If ActivePart <> "" Then Result.Add(ActivePart)
            Return Result

        End Function

        ''' <summary>
        ''' Combines all of the parts of this item into a single string, each part separated by two newlines.
        ''' </summary>
        ''' <returns>A single string that describes all of the parts that make up the item.</returns>
        Public Function CombineIntoString() As String

            Dim Result As New StringBuilder
            For I As Integer = 0 To Me.Parts.Count - 1
                Dim Part As String = Me.Parts(I)
                Result.Append(Part)
                If I < Me.Parts.Count - 1 Then Result.Append(Environment.NewLine).Append(Environment.NewLine)
            Next
            Return Result.ToString

        End Function

    End Class

End Namespace